//
//  UIImage+Extension.swift
//  JMicroBlog
//
//  Created by Jason on 29/5/15.
//  Copyright (c) 2015 Shepherd. All rights reserved.
//

import UIKit
extension UIImage {
    func scaleImage(#width : CGFloat) -> UIImage? {
       // 取得缩放图片的尺寸大小
        let size = scaleImageSize(width: width)
       // 1.开启上下文
        UIGraphicsBeginImageContext(size)
       // 2.在上下文中绘制图像,在rect中拉伸绘制
        drawInRect(CGRect(origin: CGPointZero, size: size))
       // 3.取出结果
        let result = UIGraphicsGetImageFromCurrentImageContext()
       // 4.关闭上下文
        UIGraphicsEndImageContext()
        
        return result
    }
    
    private func scaleImageSize(#width : CGFloat) -> CGSize {
        // 水平缩放的比例
        let wScale = size.width / width
        let height = size.height / wScale
        return CGSizeMake(width, height)
    }
}
